package com.fuego.creditsystemcommodity.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuego.creditsystemcommodity.service.IOwnerService;
import jakarta.annotation.Resource;
import org.example.dto.Result;
import org.example.entity.Owner;
import org.example.utils.SystemConstants;
import org.springframework.web.bind.annotation.*;




@RestController
@RequestMapping("/owner")
public class OwnerController {

    @Resource
    public IOwnerService ownerService;


    @GetMapping("/{id}")
    public Result queryOwnerById(@PathVariable("id") Long id) {
        return ownerService.queryById(id);
    }


    @PostMapping
    public Result saveOwner(@RequestBody Owner owner) {
        // 写入数据库
        ownerService.save(owner);
        // 返回店铺id
        return Result.ok(owner.getId());
    }


    @PutMapping
    public Result updateOwner(@RequestBody Owner owner) {
        // 写入数据库
        ownerService.updateById(owner);
        return ownerService.update(owner);
    }


    @GetMapping("/of/type")
    public Result queryOwnerByType(
            @RequestParam("typeId") Integer typeId,
            @RequestParam(value = "current", defaultValue = "1") Integer current

    ) {
        return ownerService.queryByType(typeId,current);
    }

    @GetMapping("/of/name")
    public Result queryOwnerByName(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "current", defaultValue = "1") Integer current
    ) {
        // 根据类型分页查询
        Page<Owner> page = ownerService.query()
                .like(StrUtil.isNotBlank(name), "name", name)
                .page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 返回数据
        return Result.ok(page.getRecords());
    }
}
